﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Freedom.Models.Interfaces;
using Freedom.Models.Entities;
using MvcPaging;

namespace Freedom.Models.Services
{
    /// <summary>
    /// 案例类型服务
    /// autor: Penn Yao
    /// </summary>
    public class PortfolioClassService : IPortfolioClassService
    {
        db_FreedomEntities db;

        public PortfolioClassService()
        {
            if(db == null)
                db = new db_FreedomEntities();
        }

        /// <summary>
        /// 添加对象
        /// </summary>
        /// <param name="obj">对象</param>
        void IPortfolioClassService.addClass(tb_PortfolioClass obj)
        {
            db.AddTotb_PortfolioClass(obj);
            db.SaveChanges();
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        bool IPortfolioClassService.deleteClass(int nID)
        {
            var obj = db.tb_PortfolioClass.FirstOrDefault(p => p.ID == nID);

            if (obj != null)
            {
                db.DeleteObject(obj);
                db.SaveChanges();

                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新对象
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        void IPortfolioClassService.updateClass(tb_PortfolioClass obj)
        {
            IQueryable<tb_PortfolioClass> allClass = db.tb_PortfolioClass;
            foreach (tb_PortfolioClass item in allClass)
            {
                if (item.ID.Equals(obj.ID))
                {
                    item.Class = obj.Class;
                    item.CreateTime = obj.CreateTime;
                    item.UpdateTime = obj.UpdateTime;
                    item.Remark = obj.Remark;
                }
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        tb_PortfolioClass IPortfolioClassService.getClass(int nID)
        {
            return db.tb_PortfolioClass.Where(p => p.ID == nID).FirstOrDefault();
        }

        /// <summary>
        /// 获取所有对象
        /// </summary>
        /// <returns></returns>
        IQueryable<tb_PortfolioClass> IPortfolioClassService.getAllClass()
        {
            return db.tb_PortfolioClass;
        }

        /// <summary>
        /// 保存
        /// </summary>
        void IPortfolioClassService.save()
        {
            db.SaveChanges();
        }
    }
}